package com.sg.java.constant;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.fs.Path;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;

public class HDFSUtil {

    public static FileSystem fs = null;

    public static void init() throws IOException {
        Configuration conf = new Configuration();
        //这个操作就是配置HDFS文件的core-site.xml
        conf.set("fs.defaultFS", "hdfs://hacluster");
        FileSystem fs = FileSystem.get(conf);
    }

    public static boolean mkdirs(String path) throws IOException {
        return fs.mkdirs(new Path(path));
    }

    public static void create(String path) throws IOException {
        fs.create(new Path(path));
    }

    public static void upload(String sourceFile, String destinationFile) throws IOException {
        fs.copyFromLocalFile(new Path(sourceFile), new Path(destinationFile));
    }

    public static void main(String[] args) throws Exception {
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
        File file = new File("C:\\Users\\HUAWEI\\Desktop\\word.txt");
        System.out.println(file.isFile());
        System.out.println(file.canRead());
        System.out.println(file.isDirectory());
        System.out.println(file.getName());
        System.out.println(file.getAbsolutePath());
        System.out.println(new URI("ftp://www.baidu.com").toURL().getProtocol());
        System.out.println(new URI("file://www.baidu.com").toURL().getProtocol());
        System.out.println(new URI("https://www.baidu.com").toURL().getProtocol());
        final URL url = new URI("https://www.baidu.com").toURL();
        final InputStream inputStream = url.openStream();
        byte[] b = new byte[1024 * 1024];     //创建合适文件大小的数组
        inputStream.read(b);    //读取文件中的内容到b[]数组
        inputStream.close();
        System.out.println(new String(b));
        System.out.println(new URI("hdfs://www.baidu.com").toURL().getProtocol());

//        File file2 = new File(new URI("https://www.baidu.com").toURL());
//        System.out.println(file2.isFile());
//        System.out.println(file2.canRead());
//        System.out.println(file2.isDirectory());
//        System.out.println(file2.getName());
//        System.out.println(file2.getAbsolutePath());
    }

}
